/**
 * RouterEntry.java
 *
 * @author Hagen Boehm
 */


package SSF.OS.OSPFv2;


import java.util.*;


// ======================== class SSF.OS.OSPFv2.RouterEntry ======================== //
/**
 *  A routing table entry describing the set of shortest paths leading to a router
 *  destination. Also included is the optional capability field of the router. All other
 *  values of these entries are encoded in the OSPFv2 Routing Table structure.
 */
public class RouterEntry {

  // ........................ member data .......................... //

  /** The optional OSPF capabilities supported by the destination router.
   */
  public boolean[] optCap;

  /** The type of path of the set of shortest paths to the destination router. The values of 
   *  pathType are specified in class OSPF.
   */
  public byte pathType;

  /** The link state cost of the paths to the router. */
  public int cost;

  /** In case of path-type INTRA_AREA this field references the LSA that directly references
   *  the destination. Here the destination is a router, so the value may only be a ROUTER_LSA.
   */
  public Router_LSA lsOrigin;

  /** A boolean value, indicating that the route destination is an AS boundary router. */
  public boolean isASBoundary;

  /** A Vector holding all equal-cost paths in form of ShortestPath objects.*/
  public Vector equalCostPaths;


  // ---------------------------- constructor RouterEntry ----------------------------- //
  /**
   * Constructs a new RouterEntry and set the fields to the given values.
   * @param opt The destination router's optional capabilities.
   * @param pType The path type of the set of shortest paths (see class OSPF).
   * @param c The cost of the shortest paths.
   * @param lso Router-LSA describing the LS Origin (currently not used in any calculations).
   * @param isASB boolean value indicating an AS boundary destination.
   * @param paths A Vector holding ShortestPath objects which uniquely identifies the various 
   *              shortest paths.
   */
  public RouterEntry(boolean[] opt, byte pType, int c, Router_LSA lso, boolean isASB, Vector paths) {
    optCap = opt;
    pathType = pType;
    cost = c;
    if (pathType == OSPF.INTRA_AREA) lsOrigin = lso;
    isASBoundary = isASB;
    equalCostPaths = paths;
  }

  // ---------------------------- RouterEntry.equals ------------------------------- //
  /** Compare this RouterEntry with another one. Return true if they are equal else false.
   *  @param routEntObj RouterEntry to compare.
   *  @return boolean value indicating whether the two entries are equal or not.
   */
  public boolean equals(Object routEntObj) {
    if (!(routEntObj instanceof RouterEntry)) return false;
    RouterEntry routEntry = (RouterEntry)routEntObj;
    if (pathType != routEntry.pathType) return false;
    if (isASBoundary != routEntry.isASBoundary) return false;
    if (cost != routEntry.cost) return false;
    if (equalCostPaths.size() != routEntry.equalCostPaths.size()) {
      return false;
    } else {
      ShortestPath sp1;
      ShortestPath sp2;
      for (Enumeration e1 = equalCostPaths.elements(); e1.hasMoreElements(); ) {
	sp1 = (ShortestPath)e1.nextElement();
        for (Enumeration e2 = routEntry.equalCostPaths.elements(); e2.hasMoreElements(); ) {
	  sp2 = (ShortestPath)e2.nextElement();
          if (!sp1.equals(sp2)) {
            if (e2.hasMoreElements()) continue;
            else return false;
	  } else {
	    break;
          }
        }
      }
    }
    return true;
  }

} // end of class RouterEntry.
